import type { UserFiltersI } from '@/types/user'
import { UserFilters } from '@/AGC/model/UserFilters'
import {
  executeDeleteUserFilters,
  normalExecuteUpsert,
  qeuryUserFilters,
} from '@/AGC/manager/user_filters'

/**
 * 创建用户筛选条件
 * @param filters 用户筛选条件
 */
export const createUserFiltersUtil = async (filters: UserFiltersI) => {
  const userFiltersModel = new UserFilters()
  userFiltersModel.setUser_id(filters.user_id)
  userFiltersModel.setTarget(filters.target)
  userFiltersModel.setSex(filters.sex)
  userFiltersModel.setAge(filters.age)
  userFiltersModel.setHeight(filters.height)
  userFiltersModel.setWeight(filters.weight)
  userFiltersModel.setTarget_weight(filters.target_weight)
  userFiltersModel.setSport_ability(filters.sport_ability)
  userFiltersModel.setEat_scene(filters.eat_scene)
  userFiltersModel.setDate(filters.date)

  await normalExecuteUpsert(userFiltersModel)
}

/**
 * 获取用户筛选条件
 * @param user_id 用户ID
 * @returns 用户筛选条件
 */
export const getUserFilterUtil = async (user_id: number) => {
  // 查询用户筛选条件
  const result = await qeuryUserFilters(user_id)

  if (result.length > 0) {
    const userFilter = result[0]

    return {
      user_id: userFilter.getUser_id()!,
      target: userFilter.getTarget()!,
      sex: userFilter.getSex()!,
      age: userFilter.getAge()!,
      height: userFilter.getHeight()!,
      weight: userFilter.getWeight()!,
      target_weight: userFilter.getTarget_weight()!,
      sport_ability: userFilter.getSport_ability()!,
      eat_scene: userFilter.getEat_scene()!,
      date: userFilter.getDate()!,
    }
  }
}

/**
 * 删除用户筛选条件
 * @param userFilters 用户筛选条件
 */
export const deleteUserFiltersUtil = async (userFilters: UserFiltersI) => {
  const userFiltersModel = new UserFilters()
  userFiltersModel.setUser_id(userFilters.user_id)
  await executeDeleteUserFilters(userFiltersModel)
}
